package cn.com.huzo.webapp.controller.trade;

import cn.com.huzo.model.entities.trade.ShippingOrderHeaderEntity;
import cn.com.huzo.model.entities.trade.ShippingOrderLinesEntity;
import huzofw.common.web.controller.BaseFormController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.com.huzo.service.trade.ShippingOrderHeaderManager;
import cn.com.huzo.service.trade.ShippingOrderLinesDoManager;
import cn.com.huzo.service.trade.ShippingOrderLinesManager;
import org.apache.commons.collections4.MapUtils;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 外贸箱单画面控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/trade/packing_list")
public class PackingListController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "ShippingOrderHeaderManager")
    private ShippingOrderHeaderManager _manager_header;

    @Resource(name = "ShippingOrderLinesDoManager")
    private ShippingOrderLinesDoManager _manager_lines_do;

    @Resource(name = "ShippingOrderLinesManager")
    private ShippingOrderLinesManager _manager_lines;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到外贸箱单一览画面
     * <p>
     * @param request 上下文请求
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("trade.packing_list.list.def");
        return view;
    }

    /**
     * 跳转外贸箱单编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作标志 1;增加 2：编辑
     * @param headerId 外贸箱单标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/{action}/{headerId}/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "headerId") Long headerId) throws Exception {
        ModelAndView view = new ModelAndView();
        view.addObject("action", action);
        view.addObject("headerId", headerId.toString());
        view.setViewName("trade.packing_list.edit.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表处理部分">
    /**
     * 得到新的单据号
     * <p>
     * @return @throws Exception
     */
    @RequestMapping(value = "/header/fetchNewBillNo.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchNewBillNo() throws Exception {
        Map<String, Object> rtnVal = new HashMap();
        rtnVal.put("BillNo", _manager_header.fetchNewBillNo());
        return rtnVal;
    }

    /**
     * 根据ID取得外贸箱单数据。
     *
     * @param criterion 外贸箱单ID、当前用户、流程标识
     * @return 发货发令单数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchByHeaderKey(MapUtils.getLong(criterion, "HeaderId"));
    }

    /**
     * 根据条件取得外贸箱单信息分页数据。
     * <p>
     * @param filters 检索条件
     * @return 分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/header/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> filters) throws Exception {
        return _manager_header.fetchForPage(filters);
    }

    /**
     * 保存外贸箱单信息数据。
     * <p>
     * @param instance 要保存的数据
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody ShippingOrderHeaderEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getHeaderId() > 0) { //编辑
            rowsAffected = (long) _manager_header.update(instance);
        } else {//新增
            rowsAffected = _manager_header.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除外贸箱单信息。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细处理部分">
    /**
     * 根据主记录ID得到全部明细数据列表
     *
     * @param headerId 主记录ID
     * @return 全部明细数据列表
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/lines/fetchAll.html",
            method = RequestMethod.POST,
            produces = {"application/xml", "application/json"})
    public @ResponseBody
    Map<String, Object> fetchLinesByHeaderId(@RequestBody final Long headerId) throws Exception {
        return _manager_lines.fetchByHeaderId(headerId);
    }

    /**
     * 更新外贸箱单明细信息数据。
     * <p>
     * @param criterion 要更新的数据。
     * @return 影响的行数。
     * @throws Exception
     */
    @RequestMapping(value = "/lines/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> saveLines(@RequestBody final Map<String, ? extends List<ShippingOrderLinesEntity>> criterion) throws Exception {
        return _manager_lines.saveLines(criterion);
    }

    /**
     * 删除外贸箱单明细信息。
     *
     * @param criterion 删除条件
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);
    }
    // </editor-fold>
}
